package com.zq.create.singleton;

// 饿汉模式
class SingletonA {
    private static final SingletonA instance = new SingletonA();

    private SingletonA() {}

    public static SingletonA getInstance() {
        return instance;
    }
}

// 懒汉模式
class SingletonB {
    private static SingletonB instance;

    private SingletonB() {}

    public static synchronized SingletonB  getInstance() {
        if (instance == null) {
            instance = new SingletonB();
        }

        return instance;
    }
}

// double check
class SingletonC {
    private static volatile SingletonC instance;

    private SingletonC() {}

    public static SingletonC  getInstance() {
        if (instance == null) {
            synchronized (SingletonC.class) {
                if (instance == null) {
                    instance = new SingletonC();
                }
            }
        }

        return instance;
    }
}

// 内部类
class SingletonD {
    private SingletonD() {}

    public static SingletonD  getInstance() {
        return InnerHelper.instance;
    }

    private static class InnerHelper {
        private static SingletonD instance = new SingletonD();
    }
}

// 枚举
enum SingletonE {
    INSTANCE {
        @Override
        public void func() {
            System.out.println("hello world");
        }
    };

    public abstract void func();

    public static SingletonE getInstance() {
        return INSTANCE;
    }
}